<template>
    <!-- pages/changeDoor/changeDoor.wxml -->
    <view class="container">
        <van-cell-group>
            <van-cell title="所属门店" :value="orderInfo.storeName" />
            <van-cell title="当前房间" :value="orderInfo.roomName" />
            <van-cell title="开始时间" :value="orderInfo.startTime" />
            <van-cell title="结束时间" :value="orderInfo.endTime" />
        </van-cell-group>
        <view class="lists">
            <view class="title border-primary">房间</view>
            <view class="doors" v-if="doorList.length">
                <!-- 禁用状态无法更换 -->
                <view
                    :class="
                        'door ' +
                        (roomType >= item.type || item.status == 0 || item.type > orderInfo.roomType ? 'disabled' : '') +
                        ' ' +
                        (item.roomId === roomId ? 'bg-primary' : '')
                    "
                    @tap="choose"
                    :data-info="item"
                    v-for="(item, index) in doorList"
                    :key="index"
                >
                    <view class="name">
                        {{ item.roomName }}（{{ item.type === 1 ? '小包' : item.type === 2 ? '中包' : item.type === 3 ? '大包' : item.type === 4 ? '豪包' : '商务包' }}）
                    </view>

                    <block v-if="item.roomId === orderInfo.roomId">
                        <view class="current"><image :src="userinfo.avatar" mode="widthFix" /></view>
                    </block>

                    <block v-else>
                        <view class="status" v-if="item.roomId === roomId">当前选中</view>
                        <view class="status" v-else>
                            {{ item.status == 1 ? '空闲' : item.status == 2 ? '待清洁' : item.status == 3 ? '使用中' : item.status == 4 ? '已预约' : '禁用' }}
                        </view>
                    </block>
                </view>
            </view>
        </view>
        <view class="note" @tap="call">
            <view class="iconfont icon-kefu"></view>
            <text>如有疑问请点击联系客服</text>
        </view>
        <view class="submit bg-primary theme-color-shadow" @tap="submit">确定更换</view>
    </view>
</template>

<script>
// pages/changeDoor/changeDoor.js
const app = getApp();
var http = require('../../utils/http');
var util1 = require('../../utils/util.js');
var Moment = require('../../lib/moment.js');
export default {
    data() {
        return {
            userinfo: {
                avatar: ''
            },
            orderInfo: {
                storeName: '',
                roomName: '',
                startTime: '',
                endTime: '',
                roomType: 0,
                roomId: ''
            },
            doorList: [],
            roomId: '',
            roomType: '' //房间类型
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad(options) {
        var orderInfo = JSON.parse(options.orderInfo);
        console.info('房间信息111===');
        console.info(orderInfo);
        this.setData({
            orderInfo: orderInfo
        });
        this.getuserinfo();
        this.getDoorList();
        if (options.roomType) {
            this.setData({
                roomType: orderInfo.roomType
            });
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {},
    methods: {
        getuserinfo: function () {
            var that = this;
            if (app.globalData.isLogin) {
                http.request(
                    '/member/user/get',
                    '1',
                    'get',
                    {},
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('我的信息===');
                        console.info(info);
                        if (info.code == 0) {
                            that.setData({
                                userinfo: info.data
                            });
                        }
                    },
                    function fail(info) {}
                );
            } else {
                //console.log('未登录失败！')
            }
        },

        //获取房间列表
        getDoorList: function (e) {
            var that = this;
            //if (app.globalData.isLogin)
            {
                http.request(
                    '/member/index/getRoomInfoList/' + that.orderInfo.storeId,
                    '1',
                    'post',
                    {
                        storeId: that.orderInfo.storeId
                    },
                    app.globalData.userDatatoken.accessToken,
                    '获取中...',
                    function success(info) {
                        console.info('房间数组===');
                        console.info(info);
                        if (info.code == 0) {
                            that.setData({
                                doorList: info.data
                            });
                        } else {
                            uni.showModal({
                                content: info.msg,
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        },

        call: function () {
            let that = this;
            if (that.orderInfo.kefuPhone.length > 0) {
                //console.log("拨打电话+++")
                uni.makePhoneCall({
                    phoneNumber: that.orderInfo.kefuPhone,
                    success: function () {
                        //console.log("拨打电话成功！")
                    },
                    fail: function () {
                        //console.log("拨打电话失败！")
                    }
                });
            }
        },

        choose: function (e) {
            var that = this;
            var info = e.currentTarget.dataset.info;
            var roomId = info.roomId;
            //禁用状态无法更换
            if (that.roomType >= info.type || info.status == 0 || info.type > this.orderInfo.roomType) {
                return;
            }
            this.setData({
                roomId: roomId
            });
        },

        submit: function () {
            var that = this;
            var roomId = that.roomId;
            var orderId = that.orderInfo.orderId;
            if (roomId === '') {
                uni.showToast({
                    title: '请选择房间',
                    icon: 'none'
                });
                return;
            }
            if (app.globalData.isLogin) {
                http.request(
                    '/member/order/changeRoom/' + orderId + '/' + roomId,
                    '1',
                    'post',
                    {
                        orderId: orderId,
                        roomId: roomId
                    },
                    app.globalData.userDatatoken.accessToken,
                    '',
                    function success(info) {
                        console.info('返回111===');
                        console.info(info);
                        if (info.code == 0) {
                            uni.showToast({
                                title: '更换房间成功'
                            });
                            setTimeout(() => {
                                uni.navigateBack();
                            }, 1000);
                        } else {
                            uni.showModal({
                                content: info.msg,
                                showCancel: false
                            });
                        }
                    },
                    function fail(info) {}
                );
            }
        }
    }
};
</script>
<style>
@import './changeDoor.css';
</style>
